package websocket

import (
	"encoding/json"
	"fmt"
	"golang.org/x/net/websocket"
	"im/im-cloud/app/data"
	ser "im/server/websocket"
)

// OnOpen 启动方法
func OnOpen(server *ser.Server, conn *websocket.Conn)  {
	fmt.Println("连接成功，启动im-cloud的Websocket")
}

// OnMessage 接收数据后，要处理的逻辑
func OnMessage(server *ser.Server, frame *ser.Frame)  {
	rec := new(data.Receive)
	_ = json.Unmarshal([]byte(frame.Data), rec)
	if dic, ok := Dispatchers[rec.Method]; ok {
		dic(server, rec)
	}else {
		server.Send("{\"code\": 404, \"msg\":\"未找到方法"+rec.Method+"\"}")
	}

}

// OnClose 关闭链接时，调用的方法
func OnClose(server *ser.Server, conn *websocket.Conn)  {
	fmt.Println("===>>> im-server 断开链接 ===>>>")
}